package com.bojiu.lawsearch.lawsearch.service.impl;

import com.bojiu.lawsearch.lawsearch.mapper.ProfessionMapper;
import com.bojiu.lawsearch.lawsearch.mapper.UnitDetailMapper;
import com.bojiu.lawsearch.lawsearch.mapper.UnitMapper;
import com.bojiu.lawsearch.lawsearch.model.Page;
import com.bojiu.lawsearch.lawsearch.model.Profession;
import com.bojiu.lawsearch.lawsearch.model.Unit;
import com.bojiu.lawsearch.lawsearch.model.UnitDetail;
import com.bojiu.lawsearch.lawsearch.service.ProfessionService;
import com.bojiu.lawsearch.lawsearch.util.UtilTools;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author: afdiao
 * @Date：2020/3/8 22:18
 * @Description:
 */
@Service
@Slf4j
public class ProfessionServiceImpl implements ProfessionService {

    @Autowired
    ProfessionMapper professionMapper;

    @Autowired
    UnitMapper unitMapper;

    @Autowired
    UnitDetailMapper unitDetailMapper;

    @Override
    public Page<Profession> getList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize, true);
        List<Profession> professions = professionMapper.selectAll();
        PageInfo pageInfo = new PageInfo(professions);
        Page page = new Page(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getPages(), pageInfo.getTotal(), professions);
        return page;
    }

    @Override
    public void updateProfession(int id, String name) {
        Profession profession = new Profession();
        profession.setName(name);
        professionMapper.updateByExampleSelective(profession, UtilTools.createExampleByKey("id", id, Profession.class));
    }

    @Override
    public void insert(Profession profession) {
        professionMapper.insertSelective(profession);
    }

    @Override
    public void delete(int id) {
        professionMapper.deleteByExample(UtilTools.createExampleByKey("id", id, Profession.class));
    }

    @Override
    public List<Profession> getAllProfession() {
        return professionMapper.selectAll();
    }


    @Override
    public List<Unit> getAllUnit() {
        return unitMapper.selectAll();
    }

    @Override
    public List<UnitDetail> getAllUnitdetailById(int unitId) {
        Example example = UtilTools.createExampleByKey("unit_id", unitId, UnitDetail.class);
        return unitDetailMapper.selectByExample(example);
    }
}
